package com.weipu.common.facade.support.service.task;

import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.facade.model.config.CacheConfigModel;
import com.weipu.common.facade.model.config.ConfigUtil;

/**
 * 获取客户端配置信息
 * @author Administrator
 *
 */
public class GetCacheConfigTask extends BaseServiceTask<CacheConfigModel>
{
    /**
     * 
     */
    private static final long serialVersionUID = 3107834471366974894L;
    
    @Override
    protected boolean executeRequest()
    {
        return true;
    }
    
    @Override
    protected CacheConfigModel execute()
    {
        CacheConfigModel config = null;
        try
        {
            config = ProviderFactory.createUserServiceProvider().getCacheConfig();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return config;
    }
    
    @Override
    protected void executeResponse(CacheConfigModel result)
    {
        updateAppConfig(result);
        
        super.executeResponse(result);
    }
    
    private void updateAppConfig(CacheConfigModel config)
    {
        if (null == config)
        {
            return;
        }
        
        //更新本地配置辅助工具
        ConfigUtil.getInstance().updateCacheCfg(config);
    }
    
    @Override
    public boolean equals(Object o)
    {
        if (!(o instanceof GetCacheConfigTask))
        {
            return false;
        }
        
        return this.toString().equals(o.toString());
    }
    
    //    equals 返回 true的链各个对象则hashcode必须相等
    @Override
    public int hashCode()
    {
        return toString().hashCode();
    }
    
    //提供Equals对比，单例任务(同一时间只需运行一个)
    @Override
    public String toString()
    {
        return "GetCacheConfigTask";
    }
    
}
